<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:p="pilferpage" xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" exclude-result-prefixes="html">

	<xsl:output method="html" omit-xml-declaration="yes" indent="yes" doctype-public="-//W3C//DTD HTML 3.2//EN"/>

	<xsl:key name="tableRows" match="p:row[not(ancestor::p:head or ancestor::p:foot)]" use="generate-id(ancestor::p:table[1])"/>

	<xsl:template match="/p:page">
		<xsl:element name="html">
			<xsl:element name="head">
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
			</xsl:element>
			<xsl:element name="body">
				<xsl:apply-templates select="p:top"/>
				<xsl:variable name="middleNodes" select="node()[not(self::p:top or self::p:left or self::p:right or self::p:bottom)]"/>
				<xsl:if test="$middleNodes or p:left or p:right">
					<table>
						<tr>
							<td><xsl:apply-templates select="p:left"/></td>
							<td><xsl:apply-templates select="$middleNodes"/></td>
							<td><xsl:apply-templates select="p:right"/></td>
						</tr>
					</table>
				</xsl:if>
				<xsl:apply-templates select="p:bottom"/>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:table">
		<xsl:element name="table">
			<xsl:variable name="numberOfNestedRows">
				<xsl:for-each select="key('tableRows', generate-id())">
					<xsl:sort select="count(ancestor::p:row)" data-type="number" order="descending"/>
					<xsl:if test="position() = 1">
						<xsl:value-of select="count(ancestor::p:row)"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:variable>

			<xsl:apply-templates select="p:title"/>
			<xsl:if test="p:head">
				<thead>
					<xsl:for-each select="p:head/p:row">
						<xsl:call-template name="drawHierarchicalRow">
							<xsl:with-param name="numberOfNestedRows" select="$numberOfNestedRows"/>
						</xsl:call-template>
					</xsl:for-each>
				</thead>
			</xsl:if>

			<xsl:if test="p:foot">
				<tfoot>
					<xsl:for-each select="p:head/p:row">
						<xsl:call-template name="drawHierarchicalRow">
							<xsl:with-param name="numberOfNestedRows" select="$numberOfNestedRows"/>
						</xsl:call-template>
					</xsl:for-each>
				</tfoot>
			</xsl:if>
			
			<tbody>
				<xsl:choose>
					<xsl:when test="p:row/p:row | p:body/p:row/p:row">
						<xsl:for-each select="key('tableRows', generate-id())">
							<xsl:call-template name="drawHierarchicalRow">
								<xsl:with-param name="numberOfNestedRows" select="$numberOfNestedRows"/>
							</xsl:call-template>
						</xsl:for-each>
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates select="p:row | p:body"/>
					</xsl:otherwise>
				</xsl:choose>
			</tbody>

		</xsl:element>
	</xsl:template>

	<xsl:template name="drawHierarchicalRow">
		<xsl:param name="numberOfNestedRows"/>
		<tr>
			<xsl:variable name="depth" select="count(ancestor::p:row)"/>
			<xsl:for-each select="descendant::*[position() &lt; $depth + 1]">
				<td>&#160;&#160;&#160;</td>
			</xsl:for-each>
			<xsl:choose>
				<xsl:when test="ancestor::p:head or ancestor::p:foot">
					<th>
						<xsl:if test="$numberOfNestedRows - $depth &gt; 0">
							<xsl:attribute name="colspan"><xsl:value-of select=" $numberOfNestedRows - $depth + 1 "/></xsl:attribute>
						</xsl:if>
						<xsl:apply-templates select="p:cell[1]/node()"/>
						<xsl:text> </xsl:text>
					</th>
				</xsl:when>
				<xsl:otherwise>
					<td>
						<xsl:if test="$numberOfNestedRows - $depth &gt; 0">
							<xsl:attribute name="colspan"><xsl:value-of select=" $numberOfNestedRows - $depth + 1 "/></xsl:attribute>
						</xsl:if>
						<xsl:apply-templates select="p:cell[1]/node()"/>
						<xsl:text> </xsl:text>
					</td>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="p:cell[position() &gt; 1]"/>
		</tr><xsl:text>&#10;</xsl:text>
	</xsl:template>


	<xsl:template match="p:row">
		<xsl:element name="tr">
			<xsl:choose>
				<xsl:when test="p:row">
					<xsl:for-each select="f"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="p:cell"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
		<xsl:apply-templates select="p:row"/>
	</xsl:template>

	<xsl:template match="p:cell">
		<xsl:choose>
			<xsl:when test="ancestor::p:head or ancestor::p:foot">
				<xsl:element name="th">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="td">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template match="p:title">
		<xsl:choose>
			<xsl:when test="parent::p:table">
				<caption><xsl:apply-templates/></caption>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="ancestorSections" select="ancestor::p:section"/>
				<xsl:choose>
					<xsl:when test="count($ancestorSections) &gt; 4">
						<h6><xsl:apply-templates/></h6>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 3">
						<h5><xsl:apply-templates/></h5>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 2">
						<h4><xsl:apply-templates/></h4>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 1">
						<h3><xsl:apply-templates/></h3>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 0">
						<h2><xsl:apply-templates/></h2>
					</xsl:when>
					<xsl:otherwise>
						<h1><xsl:apply-templates/></h1>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>	

	<xsl:template match="p:paragraph">
		<xsl:element name="p"><xsl:apply-templates/></xsl:element>
	</xsl:template>

	<xsl:template match="p:link">
		<xsl:element name="a">
			<xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

</xsl:stylesheet>
